<template>
  <div class="resumeItemContainer">
    <div class="resumeItemTitle">
      <div class="resumeItemTitleFlag"></div>
      <span class="resumeItemTitleContent">个人证书</span>
      <span class="resumeItemTitleAddIcon" @click="handleAddClick"><img :src="resumeItemAddIcon"
          class="resumeItemAddIcon"> 添加</span>
    </div>
    <div class="resumeItemMain">
      <div class="resumeItemDisplayList">
        <div class="resumeItemDisplayItem" v-for="item,index in resumeDataList" :key="item.id">
          <div class="resumeItemDisplayItemEditButtons">
            <template v-if="!item.editStatus">
              <!-- <span class="resumeItemDisplayItemEditButton" @click="changeEditStatus(item,true)">
                <img :src="myResumeEditIcon" /> 编辑</span>
              <span class="resumeItemDisplayItemEditButton" @click="handleDeletClick()">
                <img :src="myResumeDeleteIcon" />删除</span> -->
            </template>
          </div>
          <div class="displayBlock" v-if="!item.editStatus">
            <!-- 非编辑状态 -->
            <div class="displayInfo">
              <span class="displayBlockItem" @click="changeEditStatus(item,true)"> {{item.CertName}} </span>
            </div>
          </div>
          <div class="editBlock" v-else :id="item.editStatus?'edittingBlock':''">
            <div class="resumeItemEdit">
              <el-form ref="certForm" :model="item.editForm" inline :rules="rules">
                <el-form-item prop="date" label="获得时间">
                  <el-date-picker v-model="item.editForm.date" type="month" placeholder="请选择获得时间"
                    @change="handleDateChange" format="yyyy-MM" value-format="yyyy-MM-dd"
                    :style="{width:'100%'}"></el-date-picker>
                </el-form-item>
                <el-form-item label="证书类型" prop="CerList">
                  <el-select v-model="item.editForm.CerList" placeholder="请选择证书类型"
                    @change="(e)=>{handleCerListChange(e,item)}">
                    <el-option v-for="item in certificateList" :key="item.value" :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="证书名称" prop="CertName">
                  <el-input v-model="item.editForm.CertName" placeholder="选填证书名称" maxlength="20"
                    v-if="item.editForm.CerList=='自定义'"></el-input>
                  <el-select v-model="item.editForm.CertName" placeholder="请选择证书名称" v-else>
                    <el-option v-for="item in item.editForm.CertNameList" :key="item.value" :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="证书成绩" prop="Scores">
                  <el-input v-model="item.editForm.Scores" placeholder="选填证书成绩" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="上传证书图片" prop="filepath">
                  <el-upload class="filepath-uploader" action="//upload.yl1001.com/upload/images/save"
                    :show-file-list="false" :on-success=" (res,file)=>{handleFilepathSuccess(res,file,item)}"
                    :before-upload="  (res,file)=>{beforeFilepathUpload(res,file,item)}">
                    <img v-if="item.editForm.filepath" :src="item.editForm.filepath" class="avatarEditImg">
                    <div class="avatar-uploader-button" v-else>
                      <i class="el-icon-plus avatar-uploader-icon"></i>
                    </div>
                  </el-upload>

                </el-form-item>
                <el-form-item class="wholeLineFormItem">
                  <div class="submitButtons">
                    <el-button size="small" @click="handleCancelForItem(item)">取 消</el-button>
                    <el-button size="small" @click="handleDeleteClick(item)"
                      v-if="resumeDataList.length>1&&!item.isNew">删
                      除</el-button>
                    <el-button size="small" type="primary" @click="handleSaveForItem(item)">完 成</el-button>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import myResumeItemsMixin from "@/mixin/myResumeItemsMixin.js";
import { certificateList } from "@/utils/certificate";
export default {
  name: "certificateInfo",
  mixins: [myResumeItemsMixin],
  props: {
    resumeData: {
      type: Array,
      default: () => [],
    },
    editStatus: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      certificateList,
      resumeDataList: [], //
      rules: {
        date: [
          { required: true, message: "请选择获得时间", trigger: "change" },
        ],
        CerList: [
          { required: true, message: "请选择证书类型", trigger: "change" },
        ],
        CertName: [
          { required: true, message: "请选择证书名称", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    resumeData: {
      immediate: true,
      handler(newValue, oldValue) {
        if (!newValue.length) return;
        //console.log("个人证书certificateInfo基本信息更新：", newValue);
        this.resumeDataList = newValue.map((certItem, index) => {
          if (!certItem.hasOwnProperty("editStatus")) {
            this.$set(certItem, "editStatus", false);
          }
          if (!certItem.hasOwnProperty("editForm")) {
            this.$set(certItem, "editForm", {
              date: certItem.Years + "-" + certItem.Months,
              CerList: certItem.CerList,
              CertName: certItem.CertName,
              Scores: certItem.Scores,
              filepath: certItem.filepath,
              uid: certItem.uid,
              id: certItem.id,
            });
          }
          return certItem;
        });
      },
    },
    editStatus: {
      handler(newValue, oldValue) {
        //如果新值是false要把所有的editStatus改为false
        if (newValue == false) {
          this.resumeDataList.forEach((item) => {
            item.editStatus = false;
            if (item.isNew) {
              this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1); // 新的删除
            }
          });
        }
      },
    },
  },
  methods: {
    //个人证书保存
    async handleSaveForItem(certItem) {
      const { CerList, CertName, Scores, filepath, date, id } =
        certItem.editForm;
      const [Years, Months, Days] = date.split("-");
      const finalParams = {
        op: "person_sub_busi",
        func: "updateMycer",
        version: "v5.9.18",
        forName: "iphone",
        param: {
          person_id: this.personId,
          updateArr: {
            Years,
            Months,
            CerList,
            CertName,
            Scores,
            filepath,
            uid: this.personId,
            id,
          },
        },
      };

      if (certItem.isNew) {
        finalParams.func = "addCer";
        finalParams.param.cerArr = finalParams.param.updateArr;
        delete finalParams.param.updateArr;
      }
      //console.log("修改个人证书参数：", finalParams);
      // return;
      this.resumeGeneralInterface(
        finalParams,
        () => {
          this.changeEditStatus(certItem, false);
          certItem.isNew = false;
        },
        () => {}
      );
    },
    //选择了证书类型后要找到它的children作为证书名称的选项
    handleCerListChange(val, item) {
      //console.log("证书类型选择：", val);
      this.$set(item.editForm, "CertName", "");
      item.editForm.CertNameList = certificateList.find(
        (item) => item.value === val
      ).children;
    },

    handleFilepathSuccess(res, file, item) {
      if (res.data && res.data.length) {
        this.$set(item.editForm, "filepath", res.data[0]);
        //console.log("item.editForm.filepath", item.editForm.filepath);
      }
    },
    beforeFilepathUpload(file, item) {
      //支持jpg和png格式图片
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      if (!isJPG && !isPNG) {
        this.$message.error("请上传jpg或png格式图片");
        return false;
      }
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isLt10M) {
        this.$message.error("图片大小不能超过10MB!");
        return false;
      }
      return true;
    },
    handleDateChange(val) {
      //console.log("日期选择：", val);
    },
    handleDeleteClick(certItem) {
      this.$confirm("确定删除该条个人证书吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //下面才是删除方法
          const finalParams = {
            op: "person_sub_busi",
            func: "deleteCer",
            version: "v5.9.18",
            forName: "iphone",
            param: {
              person_id: this.personId,
              cer_id: certItem.id,
            },
          };
          this.resumeGeneralInterface(
            finalParams,
            () => {},
            () => {}
          );
        })
        .catch(() => {});
    },
    handleCancelClick(item) {
      if (item.isNew) {
        this.resumeDataList.splice(this.resumeDataList.indexOf(item), 1);
      } else {
        this.$set(certItem, "editForm", {
          date: certItem.Years + "-" + certItem.Months,
          CerList: certItem.CerList,
          CertName: certItem.CertName,
          Scores: certItem.Scores,
          filepath: certItem.filepath,
          uid: certItem.uid,
          id: certItem.id,
        });
      }
      this.changeEditStatus(item, false);
    },
    handleCancelForItem(certItem) {
      if (certItem.isNew) {
        this.resumeDataList.splice(this.resumeDataList.indexOf(certItem), 1);
      }
      this.changeEditStatus(certItem, false);
    },
    changeEditStatus(item, status) {
      if (status) {
        let editingIndex = this.checkEditingItem();
        if (editingIndex != -1) {
          this.$confirm("有内容没有保存，确定退出编辑吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              //全部关闭编辑再打开当前的
              this.resumeList.forEach((item) => {
                item.editStatus = false;
              });
              this.resumeDataList.forEach((item) => {
                item.editStatus = false;
              });
              item.editStatus = status;
              this.$emit("update:editStatus", status);
              //恢复证书名称列表
              this.$set(
                item.editForm,
                "CertNameList",
                certificateList.find((_item) => {
                  return _item.value === item.CerList;
                }).children
              );
            })
            .catch(() => {});
        } else {
          item.editStatus = status;
          this.$emit("update:editStatus", status);
          //恢复证书名称列表
          this.$set(
            item.editForm,
            "CertNameList",
            certificateList.find((_item) => {
              return _item.value === item.CerList;
            }).children
          );
        }
      } else {
        //如果是设置为false则直接设置当前的状态
        item.editStatus = status;
        this.$emit("update:editStatus", status);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.resumeItemContainer {
  display: flex;
  flex-direction: column;
  margin: 20px 0 16px;
  .resumeItemTitle {
    display: flex;
    align-items: center;
    .resumeItemTitleFlag {
      width: 4px;
      height: 19px;
      background: #0b6eff;
      border-radius: 2px;
      margin-right: 10px;
    }
    .resumeItemTitleContent {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 20px;
      color: #333333;
      margin-right: 19px;
    }
    .resumeItemTitleAddIcon {
      display: flex;
      align-items: center;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #4880ff;
      cursor: pointer;
      .resumeItemAddIcon {
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }
    }
  }
  .resumeItemMain {
    margin-top: 16px;
    .resumeItemDisplayList {
      display: flex;
      flex-wrap: wrap;
      .resumeItemDisplayItem {
        position: relative;
        margin-bottom: 16px;
        margin-right: 10px;
        &:last-of-type {
          margin-bottom: 0px;
        }
        &:hover {
          .resumeItemDisplayItemEditButtons {
            display: block;
          }
        }
        .resumeItemDisplayItemEditButtons {
          display: none;
          position: absolute;
          top: 3px;
          right: 0;
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #4880ff;
          line-height: 18px;
          & > span {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            margin-left: 23px;
            user-select: none;
            & > img {
              width: 16px;
              height: 16px;
              margin-right: 5px;
            }
          }
        }
        .displayBlock {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 15px;
          color: #666666;
          line-height: 18px;
          text-align: left;
          font-style: normal;
          .displayInfo {
            display: flex;
            align-items: center;
          }
          .displayBlockItem {
            padding: 14px 16px;
            background: #f5f6f7;
            border-radius: 8px;
            border: 1px solid #fcfeff;
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 14px;
            color: #333333;
            line-height: 20px;
            text-align: left;
            font-style: normal;
            cursor: pointer;
          }
        }
        .editBlock {
          margin-top: 2px;
          background: #f8f8f8;
          border-radius: 8px;
          padding: 16px;
          ::v-deep .el-form-item {
            display: inline-flex;
            flex-direction: column;
            min-width: 251px;
            width: calc(33% - 40px);
            margin-right: 24px;
            margin-bottom: 16px;
            margin-top: 0px;
            &.wholeLineFormItem {
              width: 100%;
              margin-bottom: 0px;
              .el-form-item__content {
                display: flex;
                justify-content: space-between;
                .submitButtons {
                  width: 100%;
                  display: flex;
                  justify-content: flex-start;
                  margin-top: 11px;
                  .el-button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100px;
                    height: 30px;
                    background: #ffffff;
                    border-radius: 4px;
                    border: 1px solid #cdcdcd;
                    font-family: PingFang-SC, PingFang-SC;
                    font-weight: bold;
                    font-size: 14px;
                    text-align: left;
                    font-style: normal;
                    padding: 0;
                    &.el-button--primary {
                      width: 100px;
                      height: 30px;
                      background: #4880ff;
                      border-radius: 4px;
                    }
                  }
                }
                &::before {
                  display: none;
                }
                &::after {
                  display: none;
                }
              }
            }
            .el-form-item__label {
              height: 22px;
              font-family: PingFangSC, PingFang SC;
              font-weight: 400;
              font-size: 14px;
              color: #666660;
              line-height: 20px;
              text-align: left;
              font-style: normal;
            }
            .filepath-uploader {
              // width: 100%;
              & > div {
                // width: 100%;
              }
              .avatar-uploader-button {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 160px;
                height: 80px;
                border-radius: 4px;
                background: #d8d8d8;
                padding: 18px 24px;
                font-family: PingFangSC, PingFang SC;
                font-weight: 400;
                font-size: 16px;
                color: #666660;
                line-height: 22px;
                text-align: left;
                font-style: normal;
              }
              .avatarEditImg {
                width: 160px;
                height: 80px;
              }
            }
          }
          .submitButtons {
            display: flex;
            justify-content: flex-end;
            position: relative;
            .el-button {
              display: flex;
              justify-content: center;
              align-items: center;
              width: 100px;
              height: 30px;
              background: #ffffff;
              border-radius: 4px;
              border: 1px solid #cdcdcd;
              font-family: PingFang-SC, PingFang-SC;
              font-weight: bold;
              font-size: 14px;
              text-align: left;
              font-style: normal;
              padding: 0;
              &.el-button--primary {
                width: 100px;
                height: 30px;
                background: #4880ff;
                border-radius: 4px;
              }
            }
          }
        }
      }
    }
  }
}
</style>